<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class Adminproperties extends CI_Controller {

	var  $temp = 'backend';
	var $error ="";
	public function __construct()
	{
		parent::__construct();
		$this->load->library('parser');
		$this->load->helper('form');
		$this->load->helper(array('form', 'url'));
		$this->load->helper('language');
		$this->load->helper('html');
		$this->load->library('encrypt');
		$this->load->helper('url');
		$this->load->library('image_lib');
		$this->load->library('session');
		$this->load->library('zip');
		$this->load->library('unzip');
	}
	public function index($cateid=0,$productid=0,$id=0,$index=1){
		if(strlen($index)<1){
			$index=1;
		}
		if(!($index>0)){
			$index = 1;
		}
		if(!($productid>0)){
			$productid = 0;
		}
		if(!($cateid>0)){
			$cateid = 0;
		}
		if(!($id>0)){
			$id = 0;
		}
		if(isset($_SESSION['userkey'])){
			$this->load->model('functions_model');
			
			$data = array(
	            'username' =>  $this->session->userdata('username'),
				'ipaddress' => $this->session->userdata('session_id'),
				'error' =>$this->error,
				'modules' =>$this->functions_model->getlistfunctions_type(1)			
			);
			$this->load->model('product_model');
			$datatable=$this->product_model->loadproperties($id,$productid);
			$id = 0;
			$propertiesname = "";
			$description ="";
			foreach($datatable as $row)
			{
				$propertiesname = $row->propertiesname;
				$description = $row->description;
			}
			$TotalRow= $this->product_model->loadbannertotal($productid);
			$Pagelink=$this->config->item('base_url')."index.php/adminproperties/index/0";
			$rownum=10;
			$data = array(
	            'username' =>  $this->session->userdata('username'),
				'ipaddress' => $this->session->userdata('session_id'),
				'error' =>$this->error,
				'modules' =>$this->functions_model->getlistfunctions_type(1),
			    'cateid' => $cateid ,
				'productid' => $productid,
				'id' => $id,
				'product'=>$this->product_model->loadproduct($productid),
			    'propertiesname' => $propertiesname ,
			    'description' => $description,
				'propertiestable' => $this->product_model->loadpropertiespage($productid,$index),
				'index' => $index,
				'pagging' => $this->BuildPage($Pagelink,$TotalRow,$index,$rownum)
			);
			//$this->load->view('backend/product',$data);
			$this->parser->parse($this->temp.'/'.'dashboard', $data);
			//$this->parser->parse($this->temp.'/'.'adminproduct', $data);
		}
		else {
			redirect('admincp/index');
		}
	}
	public function addfunctions($cateid,$productid,$id=0){
		$data['modules'] = $this->functions_model->getlistfunctions_type(1);
		if(isset($_SESSION['userkey'])){
			
			//Kiá»ƒm tra khi click nÃºt cáº­p nháº­t thÃªm modules
			if ($this->input->post('submit')) {
				$namemodules = $this->input->post('modname',true);
				$this->load->model('product_model');
				//$productid=$this->input->post('dropdown_product',true);
				$propertiesname=$this->input->post('text_propertiesname',true);
				$description=$this->input->post('textarea_description',true);
				$insert = $this->product_model->updateproperties($id,$productid,$propertiesname,$description);
				redirect('adminproperties/index/'.$cateid.'/'.$productid);
			}		
		}
		else {
			redirect('adminproperties/index');
		}
	}
	
	public function delete($cateid=0,$productid=0,$id=0,$index=0)
	{
		$this->load->model('product_model');
		$insert = $this->product_model->deleteproperties($id);
		redirect("adminproperties/index/".$cateid."/".$productid."/0/".$index);
	
	}
	
	function BuildPage($PageLink,$TotalRow,$CurrentPageIndex,$PageSize)
        {
			$TotalPage = 0;
			$MaxPage=10;
			$TotalPage = floor($TotalRow / $PageSize);
			if ($TotalRow % $PageSize > 0)
			{
				$TotalPage++;
			}
			if ($CurrentPageIndex > $TotalPage)
			{
				$CurrentPageIndex = $TotalPage;
			}
			if ($CurrentPageIndex < 1)
			{
				$CurrentPageIndex = 1;
			}
			$BeginRow=($CurrentPageIndex - 1) * $PageSize + 1;;
			$EndRow=($CurrentPageIndex - 1) * $PageSize + $PageSize;
			
			$arr=explode("?",$PageLink);
			$PageLink=$PageLink."/";
			if($TotalPage<2)
			{
				return "";
			}
			
			if ($TotalPage <=$MaxPage)
			{
				$BeginPage=1;
            }
			elseif ($CurrentPageIndex == 1)
			{
				$BeginPage=1;
			}
			elseif ($CurrentPageIndex == $TotalPage)
			{
				$BeginPage= $TotalPage + 1 - $MaxPage;
			}
			elseif ($CurrentPageIndex + $MaxPage / 2 >= $TotalPage)
			{
				$BeginPage= $TotalPage + 1 - $MaxPage;
			}
			elseif ($CurrentPageIndex - $MaxPage / 2 <= 1)
			{
				$BeginPage= 1;
			}
			else
			{
            	$BeginPage=$CurrentPageIndex - $MaxPage / 2;
			}
			$Return="<div class=\"jb_pagination\">";
			if ($TotalPage > $MaxPage)
			{
				$Return=$Return . "<a href=\"" . (string)PageLink . "1\">Đầu</a>";
				for ($i = 0; $i < $MaxPage; $i++)
				{
					if ($BeginPage + $i == $CurrentPageIndex)
					{
						$Return=$Return . "<a class=\"p_current\" href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
					else
					{
						$Return=$Return . "<a href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
				}
				$Return=$Return . "<a href=\"" . (string)$PageLink . (string)$TotalPage . "\">Cuối</a>";
			}
			else
			{
				for ($i = 0; $i < $TotalPage; $i++)
				{
					if ($BeginPage + $i == $CurrentPageIndex)
					{
						$Return=$Return . "<a class=\"active\" href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
					else
					{
						$Return=$Return . "<a href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
				}
			}

			$Return=$Return . "</div>";

			return $Return;
        }
}
